package com.qiuling.iot.config;


import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;


/**
 * @Author：zhangqiang
 * @Description：swagger2配置
 * @Date：Created in 20:07 2020/2/7
 * @Modified By：
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Value("${swagger2.enable}")
    private boolean enable;

    @Bean
    public Docket createRestApi() {
        /**
         * 该集合作用是给swagger2测试接口的时候添加头部信息
         */
        ArrayList<Parameter> pars = Lists.newArrayList();
        //token参数
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name("authorization")
                .description("swagger测试用（模拟authorization传入）非必填header")
                .modelRef(new ModelRef("String"))
                .parameterType("header")
                .required(false);

        //需要刷新的token参数
        ParameterBuilder refreshTokenPar = new ParameterBuilder();
        refreshTokenPar.name("refresh_token")
                .description("swagger测试用（模拟刷新token传入）非必填header")
                .modelRef(new ModelRef("String"))
                .parameterType("header")
                .required(false);
        pars.add(tokenPar.build());
        pars.add(refreshTokenPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.qiuling.iot.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars)
                .enable(enable);

    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("秋灵智慧互联")
                .description("秋灵物联网平台")
                .termsOfServiceUrl("com.qiuling.iot")
                .version("1.0.0-SNOPSHOT")
                .build();
    }
}
